<template>
	<div>
		<van-field v-model="v_text_model" center clearable :label="componentModel.label" readonly v-show="visible">
			<template #button>
				<van-button size="mini" type="primary" @click="onClickHandler" v-show="!readonly">查询</van-button>
			</template>
		</van-field>
		<input type="hidden" v-model="v_value_model"/>
		<van-popup v-model:show="popupShow" closeable :style="{ height: '100%',width:'100%' }" :lazy-render="false">
			<mobile-page-component  ref="pageComponent">
			</mobile-page-component>
		</van-popup>
	</div>
</template>

<script>
    import {sendComponentValueChangeRequest,sendComponentEventRequest} from '@/utils/requestUtils.js';
	import utils from '@/utils/utils.js';
	import {
		getPageData
	} from '@/utils/pageUtils.js';
	export default {
		name: "SearchComponent",
		props: ["componentModel", "scope"],
		data() {
			return {
				v_text_model: "",
				v_value_model: "",
				datamodel: {},
				visible: true,
				readonly: false,
				label:"",
				popupShow:false,
			}
		},
		inject:['children'],
		mounted() {
			this.children.push(this);
		},beforeUnmount() {
			utils.removeArrayElement(this.children,this);
		},
		methods: {
			setProp(prop) {
				var dataModel = prop["options"];
				if (dataModel != undefined) {
					this.datamodel = dataModel;
				}
				for (var k in prop) {
					if (k == "value") {
						this.v_value_model = prop[k];
						for (var i = 0; i < this.datamodel.length; i++) {
							if (this.datamodel[i].value == this.v_value_model) {
								this.v_text_model = this.datamodel[i].name;
								break;
							}
						}
					}else if(k=="visible"){
						this.visible=prop[k];
					}else if(k=="readonly"){
						this.readonly=prop[k];
					}else if(k=="label"){
						this.label=prop[k];
					}
				}
			},
			onClickHandler() {
				if(this.readonly){
					return;
				}
				this.popupShow=true;
				var pageComponent=utils.getPageComponent(this);
				var parentCode=pageComponent.componentModel.code;
				var parentData=getPageData(pageComponent,this.children);
				var dataJsonString=encodeURIComponent(JSON.stringify(parentData));
				var data={};
				data["parentCode"]=parentCode;
				data["parentData"]=dataJsonString;
				data["searchComponent"]=this.componentModel.id;
				this.$refs.pageComponent.navigatePage(this.componentModel.pageCode,data,true);
			},
			searchCallback(value, name) {
				if (value == null) {
					this.v_value_model = "";
				} else {
					this.v_value_model = value;
				}
				this.v_text_model = name;
				if (this.scope != undefined) {
					this.$emit("onValueChange", this);
					return;
				}
				sendComponentValueChangeRequest(this);
			},
			getValue() {
				return this.v_value_model == null ? "" : this.v_value_model;
			},
			setSelectedValue(value){
				sendComponentEventRequest(this,"searchValueDisplayEvent",{"value":value});
			},setVisible(visible){
				this.popupShow=visible;
			}
		},
	}
</script>

<style>
</style>
